/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.risk.setting.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hopes.bizz.common.core.util.DateUtils;
import com.hopes.bizz.risk.setting.dto.RiskCalendarDTO;
import com.hopes.bizz.risk.setting.entity.RiskCalendarEntity;
import com.hopes.bizz.risk.setting.service.RiskCalendarService;
import com.hopes.bizz.risk.setting.vo.RiskCalendarVO;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 日历
 *
 * @author daijie
 * @date 2023-07-20 13:18:30
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/calendar" )
@Tag(description = "calendar" , name = "日历管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class RiskCalendarController {

    private final RiskCalendarService riskCalendarService;

    /**
     * 查询日历列表
     * @param riskCalendar 日历
     * @return
     */
    @Operation(summary = "查询日历列表" , description = "查询日历列表" )
    @GetMapping("/list" )
    @PreAuthorize("@pms.hasPermission('risk_calendar_view')" )
    public R getRiskCalendarPage(RiskCalendarDTO riskCalendar) {
        LambdaQueryWrapper<RiskCalendarEntity> wrapper = Wrappers.lambdaQuery();
        Date date = DateUtils.parseDate(riskCalendar.getDate(), "yyyy-MM");
        int dayOfWeek = DateUtils.get(date, Calendar.DAY_OF_WEEK);
        Date beginTime = DateUtils.addDate(date, -(dayOfWeek - 2));
        wrapper.ge(RiskCalendarEntity::getDateDate, beginTime);
        wrapper.le(RiskCalendarEntity::getDateDate, DateUtils.addDate(beginTime, 41));
        List<RiskCalendarEntity> riskCalendars = riskCalendarService.list(wrapper);
        List<RiskCalendarVO> calendars = new ArrayList<>();
        for (RiskCalendarEntity riskCalendarEntity : riskCalendars) {
            RiskCalendarVO vo = new RiskCalendarVO();
            BeanUtils.copyProperties(riskCalendarEntity, vo);
            vo.setDayOfMonth(DateUtils.get(vo.getDateDate(), Calendar.DAY_OF_MONTH));
            calendars.add(vo);
        }
        return R.ok(calendars);
    }

    /**
     * 通过id查询日历
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('risk_calendar_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(riskCalendarService.getById(id));
    }

    /**
     * 新增日历
     * @param riskCalendar 日历
     * @return R
     */
    @Operation(summary = "新增日历" , description = "新增日历" )
    @SysLog("新增日历" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('risk_calendar_add')" )
    public R save(@RequestBody RiskCalendarEntity riskCalendar) {
        return R.ok(riskCalendarService.save(riskCalendar));
    }

    /**
     * 修改日历
     * @param riskCalendar 日历
     * @return R
     */
    @Operation(summary = "修改日历" , description = "修改日历" )
    @SysLog("修改日历" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('risk_calendar_edit')" )
    public R updateById(@RequestBody RiskCalendarEntity riskCalendar) {
        return R.ok(riskCalendarService.updateById(riskCalendar));
    }

    /**
     * 通过id删除日历
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除日历" , description = "通过id删除日历" )
    @SysLog("通过id删除日历" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('risk_calendar_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(riskCalendarService.removeBatchByIds(CollUtil.toList(ids)));
    }
}